<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreServiceFactory" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreService" %>
<%@ page import="com.google.appengine.api.datastore.Query" %>
<%@ page import="com.google.appengine.api.datastore.Entity" %>
<%@ page import="com.google.appengine.api.datastore.FetchOptions" %>
<%@ page import="com.google.appengine.api.datastore.Key" %>
<%@ page import="com.google.appengine.api.datastore.KeyFactory" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<html>
  <head>
    <link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
  </head>

  <body>
<%
    String sector = request.getParameter("sector");
    if (sector == null) {
        sector = "default";
    }
    pageContext.setAttribute("sector", sector);
    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();
    if (user != null) {
      pageContext.setAttribute("user", user);
%>
<p>Hello, ${fn:escapeXml(user.nickname)}! (You can
<a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">sign out</a>.)</p>
<%
    } else {
%>
<p>Hello!
<a href="<%= userService.createLoginURL(request.getRequestURI()) %>">Sign in</a>
to include your name with tiks you post.</p>
<%
    }
%>



<%
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Key sectorKey = KeyFactory.createKey("Sector", sector);
    // Run an ancestor query to ensure we see the most up-to-date
    // view of the Tiktolks belonging to the selected Sector.
    Query query = new Query("tiktolk", sectorKey).addSort("date", Query.SortDirection.DESCENDING);
    List<Entity> Tiktolks = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(5));
    //if (query == null) {
    if (Tiktolks.isEmpty()) {
        %>
        <p>Location '${fn:escapeXml(sector)}' has no Tiktolks.</p>
        <br/><br/><br/>
        <%
    } else {
        %>
        <p>available TikTolks in location: '${fn:escapeXml(sector)}'.</p>
        <br/><br/><br/>
        <%
        for (Entity Tiktolk : Tiktolks) {
            pageContext.setAttribute("Tiktolk_content",
                                     Tiktolk.getProperty("tiktolkName"));
            
            %>
            
            <form action="tik.jsp" method="post">
      <div><input type="submit" value="join ${fn:escapeXml(Tiktolk_content)}" /></div>
      <input type="hidden" name="tiktolkName" value="${fn:escapeXml(Tiktolk_content)}"/>
      <input type="hidden" name="sector" value="${fn:escapeXml(sector)}"/>
    </form>
    <%
    if (Tiktolk.getProperty("user") == null) {
                %>
                <p>Was created by anonymous person</p>
                <%
            } else {
                pageContext.setAttribute("Tiktolk_user",
                                         Tiktolk.getProperty("user"));
                %>
                <p>Was created by<b>${fn:escapeXml(Tiktolk_user.nickname)}</b></p>
                <%
            }
          %><br/>
          <% 
        }
    }
%>

	<div> create a new Tiktolk</div>
    <form action="/Tiktolk" method="post">
      <div><textarea name="tiktolkName" rows="1" cols="20"></textarea></div>
      <div><input type="submit" value="create" /></div>
      <input type="hidden" name="location" value="${fn:escapeXml(location)}"/>
      <input type="hidden" name="sector" value="${fn:escapeXml(sector)}"/>
    </form>
    

  </body>
</html>